package com.bawei.module_login

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.RoutePath
import com.bawei.lib_common.mvi.BaseMviActivity
import com.bawei.module_login.databinding.ActivityLoginBinding
import com.bawei.module_login.inient.LoginIntent
import com.bawei.module_login.status.LoginStatus
import com.bawei.module_login.vm.LoginViewModel
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@AndroidEntryPoint
@Route(path = RoutePath.PATH_LOGIN_PAGE)
class LoginActivity : BaseMviActivity<ActivityLoginBinding>() {

    //设置viewmodels
    private val  loginviewmodel:LoginViewModel by viewModels()
    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun initViews() {
        mDataNBinding.btnLogin.setOnClickListener {
            lifecycleScope.launch {
                loginviewmodel.channel.send(LoginIntent.LoadLogin(mDataNBinding.etUsername.text.toString(),mDataNBinding.etPassword.text.toString()))
            }
        }
    }

    override suspend fun collectUIState() {
        loginviewmodel.uiStateFlow.collect{
            when(it){
               is LoginStatus.LoadloginSuccess->{
                   ToastUtils.showShort("登陆成功")
                   ARouter.getInstance().build(RoutePath.PATH_MAIN_PAGE).navigation()
               }
                is LoginStatus.LoadloginFail->{
                    ToastUtils.showShort(it.errorMsg)
                }
                else->{

                }
            }
        }
    }


}